package com.badibadi.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.badibadi.activity.CalendarClubActivityActivity.getList;
import com.badibadi.activity.ConsultingEvaluationActivity;
import com.badibadi.adapter.AfterLandingMyHomePageAllAdapter_1;
import com.badibadi.adapter.Popwindows_Adapter;
import com.badibadi.infos.Activity_Overview_Model;
import com.badibadi.infos.ClueTypeModel;
import com.badibadi.infos.Consult_Model;
import com.badibadi.infos.ListsModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.TempTools;
import com.badibadi.mytools.Utils;
import com.badibadi.mytools.ViewHolder;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.TextView;

public class ConsultingFragment0 extends BaseFragment implements
		IXListViewListener {

	private XListView xListView1;
	private ConsultingAdapter Adapter;
	private int Page;// 当前显示数据页数
	private int Type = 1;// 显示类型,0
	private int PageNum = 10;// 一页显示多少个
	private String type_id = null, content = null, uid, cid;
	private double jingdu, weidu;
	private int languageType = 0;
	private MySharePreferences preferences;
	private List<Consult_Model> consult_Model;
	private List<Consult_Model> list;
	private ListsModel listsModel;
	private Results results;
	private ClueTypeModel model;
	private Message message;
	private Bundle bundle;
	
	private String LanguageType;// 语言区分
	String TAG = "ConsultingFragment0";

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_net_error));
					xListView1.stopRefresh();
					xListView1.stopLoadMore();
					xListView1.setRefreshTime(""
							+ getResources().getString(R.string.ganggang));
				} catch (Exception e) {
					
				}

				break;
			case 2:
				try {
					Utils.ExitPrgress(getActivity());

					consult_Model.addAll(list);
					Adapter.notifyDataSetChanged();
					xListView1.stopRefresh();
					xListView1.stopLoadMore();
					xListView1.setRefreshTime(""
							+ getResources().getString(R.string.ganggang));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_xa10));
					xListView1.stopRefresh();
					xListView1.stopLoadMore();
					xListView1.setRefreshTime(""
							+ getResources().getString(R.string.ganggang));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 4:
				try {
					Utils.ExitPrgress(getActivity());
					
				} catch (Exception e) {

				}

				break;

			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	public View onCreateView(final LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.consult_listview, null);
		Page =1;
		

		LanguageType = Dialog.getSystemLanguageTypegrzx(getActivity());
		try {

			uid = getArguments().getString("uid");
			cid = getArguments().getString("cid");
			Type = getArguments().getInt("type");
		} catch (Exception e) {
			// TODO: handle exception
		}

		consult_Model = new ArrayList<Consult_Model>();
		Adapter = new ConsultingAdapter(consult_Model, getActivity());
		preferences = new MySharePreferences(getActivity(), "language");
		languageType = preferences.get("language", 0);
		InitMyXListView1(view);
		consult(Type, Page);
		return view;
	}

	public void InitMyXListView1(View view) {
		xListView1 = (XListView) view.findViewById(R.id.my_consult_xListView1);
		xListView1.setBackgroundColor(getResources().getColor(
				R.color.gray_eaeaea));
		xListView1.setPadding(10, 10, 10, 10);
		xListView1.setXListViewListener(this);
		xListView1.setPullLoadEnable(true);
		xListView1.setPullRefreshEnable(true);
		xListView1.setAdapter(Adapter);
		xListView1.setFocusable(true);
		xListView1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(),
						ConsultingEvaluationActivity.class);

				intent.putExtra("uid", consult_Model.get(position-1).getId());
				startActivity(intent);

			}
		});

	}

	@Override
	public void onRefresh() {
		Page = 1;
		if(consult_Model!=null){
			consult_Model.clear();
		}
		
		consult(Type, Page);
	}

	@Override
	public void onLoadMore() {
				
			consult(Type, ++Page);
		
	}

	private void consult(final int type, final int page) {

		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				list = new ArrayList<Consult_Model>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", type);
				map.put("page", page);
				map.put("pageNum", PageNum);
				map.put("uid", uid);
				map.put("cid", cid);
				map.put("languageType", TempTools.BackLanguage(languageType));
				System.out.println(map);
				Log.i(TAG, "传值");
				String acticontent = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "club/consult");
				System.out.println("acticontent" + acticontent);
				if (acticontent != null) {
					 results = Utils.checkResult_NNN(getActivity(),
							acticontent);
//					   
					 if (results != null && !results.getRetmsg().equals("null")
								&& results.isRet()) {
						// ("结果是:" + results.getRetmsg());
						try {
							list = JSONUtils.getListByJsonString(
									results.getRetmsg(), Consult_Model.class);
							System.out.println("zixun" + results.getRetmsg());
							
							mHandler.sendEmptyMessage(2);
						} catch (Exception e) {
							

						}
					} else {
						if (page > 1) {
							--Page;
						} else {
							Page = 1;
						}
						Log.i(TAG, "传值");
						mHandler.sendEmptyMessage(3);
						Log.i(TAG, "传值2");
						
					}
				} else {
					if (page > 1) {
						--Page;
					} else {
						Page = 1;
					}
					mHandler.sendEmptyMessage(1);
				}
			}
		});
	}

	// 自定义ListView的适配器
	public class ConsultingAdapter extends BaseAdapter {
		private DisplayImageOptions options;
		private List<Consult_Model> consult_Model;
		private Context context;

		// FinalBitmap finalBitmap;
		// private int Type;

		public ConsultingAdapter(List<Consult_Model> consult_Model,
				Context context/* , int t */) {
			options = new DisplayImageOptions.Builder()
					.showStubImage(R.drawable.ic_stub)
					.showImageForEmptyUri(R.drawable.noimage)
					.showImageOnFail(R.drawable.noimage).cacheInMemory()
					.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5))
					.build();
			this.consult_Model = consult_Model;
			this.context = context;
			// Type = t;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return consult_Model.size();
			// return 30;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return consult_Model.get(position);
			// return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = ViewHolder.buildView(context,
						R.layout.activity_consulting, parent);
			}
			ViewHolder viewHolder = (ViewHolder) convertView.getTag();
			ImageView activity_consulting_head = (ImageView) convertView
					.findViewById(R.id.activity_consulting_head);
			TextView activity_consulting_name = (TextView) convertView
					.findViewById(R.id.activity_consulting_name);
			RatingBar activity_consulting_xing = (RatingBar) convertView
					.findViewById(R.id.activity_consulting_xing);
			TextView activity_consulting_pinlunNumber = (TextView) convertView
					.findViewById(R.id.activity_consulting_pinlunNumber);
			TextView activity_consulting_country = (TextView) convertView
					.findViewById(R.id.activity_consulting_country);
			TextView activity_consulting_city = (TextView) convertView
					.findViewById(R.id.activity_consulting_city);
			TextView activity_consulting_qu = (TextView) convertView
					.findViewById(R.id.activity_consulting_qu);
			TextView activity_consulting_money = (TextView) convertView
					.findViewById(R.id.activity_consulting_money);
			TextView consult_content = (TextView) convertView
					.findViewById(R.id.consult_content);

			try {

				ImageLoader.getInstance().displayImage(
						Constants.BadiDownImgUrl
								+ consult_Model.get(position).getHead()
								+ Constants.appPhoto4img,
						activity_consulting_head, options);
			} catch (Exception e) {

			}
			activity_consulting_name.setText(consult_Model.get(position)
					.getNickName());
			activity_consulting_xing.setRating(Float
					.parseFloat(consult_Model.get(position).getTotalScore()));
			activity_consulting_xing.setFocusable(false);
			activity_consulting_xing.setFocusableInTouchMode(false);
			activity_consulting_xing.requestFocus();
			activity_consulting_pinlunNumber.setText(consult_Model
					.get(position).getTotalScore());
			activity_consulting_country.setText(consult_Model
					.get(position).getCountry());
			activity_consulting_city.setText(consult_Model
					.get(position).getSheng());
			activity_consulting_qu.setText(consult_Model
					.get(position).getCity());
			activity_consulting_money.setText(consult_Model.get(position)
					.getAdvisory_fees());
			consult_content
					.setText(consult_Model.get(position).getSpeciality());

			return convertView;
		}

	}
}
